package managedbean.staff;

import exception.ExistException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.faces.model.SelectItem;
import managedbean.PageRedirectorBean;
import org.primefaces.model.MenuModel;
import session.stateless.StaffAccountSessionBean;
import util.entityhelper.StaffAccountState;
import util.generator.StaffMenuGenerator;
import util.localization.WebLocalization;

@ManagedBean
@ViewScoped
public class StaffUnlockAccountManagedBean {

    private List<StaffAccountState> accountList;
    private String accountSelected;
    private SelectItem[] accountSelectedItems;
    @EJB
    private StaffAccountSessionBean staffAccountMgr;
    
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private StaffAccountState staffAcctState;
    
    // fields for this bean
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    
    // fields passed from session
    private Long staffAccountId;
    private String preferredLocale;
    private MenuModel staffMenuModel;
    private PageRedirectorBean redirect;
    

    public StaffUnlockAccountManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        /* pre-load values from session and database */
        // get account ID values from session
        if (ctx.getExternalContext().getSessionMap().containsKey("staffAccountId")) {
            // get session values
            staffAccountId = (Long) ctx.getExternalContext().getSessionMap().get("staffAccountId");
            // put back session values
            ctx.getExternalContext().getSessionMap().put("staffAccountId", staffAccountId);
        }
        else {
            // redirect back to login
            redirect = new PageRedirectorBean();
            redirect.goStaffLoginPageAtPreviousLevel();
        }
        /* END load values from session and database */      
    }
    

    public String getAccountSelected() {
        return accountSelected;
    }

    public void setAccountSelected(String accountSelected) {
        this.accountSelected = accountSelected;
    }

    public SelectItem[] getAccountSelectedItems() {
        return accountSelectedItems;
    }

    public void setAccountSelectedItems(SelectItem[] accountSelectedItems) {
        this.accountSelectedItems = accountSelectedItems;
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public Long getStaffAccountId() {
        return staffAccountId;
    }

    public void setStaffAccountId(Long staffAccountId) {
        this.staffAccountId = staffAccountId;
    }

    public MenuModel getStaffMenuModel() {
        return staffMenuModel;
    }

    public void setStaffMenuModel(MenuModel staffMenuModel) {
        this.staffMenuModel = staffMenuModel;
    }

    public void initView(PhaseEvent event) {
        try {
            // set language for all MANAGERS for all THROWABLE EXCEPTIONS in backend
            // to use this line everytime possible language changes
            staffAccountMgr.setSelectedLocale(preferredLocale);
            // get data from backend, exception alrd thrown by backend if these methods fail.

            staffAcctState = staffAccountMgr.getStaffAccountByID(staffAccountId);

            accountSelectedItems = new SelectItem[2];
            accountSelectedItems[0] = new SelectItem("Tourist Account", "Tourist Account");
            accountSelectedItems[1] = new SelectItem("StakeHolder Account", "StakeHolder Account");

            /* PRE-SET MENU BASED ON ACCESS RIGHTS */
            StaffMenuGenerator menuGenerator = new StaffMenuGenerator(staffAcctState);
            staffMenuModel = menuGenerator.generateStaffMenu();
            /* END PRE-SET MENU BASED ON ACCESS RIGHTS */ 
        }
        catch (ExistException ex) {
//            System.out.println(ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
    }

    public List<StaffAccountState> getAccountList() {
        return accountList;
    }

    public void setAccountList(List<StaffAccountState> accountList) {
        this.accountList = accountList;
    }
    
    public void selectAccount(ActionEvent event)
    {
        if(accountSelected.equalsIgnoreCase("Tourist Account"))
        {
            try {
                accountList = staffAccountMgr.getStaffAccounts();
            }
            catch (ExistException ex) {
                System.out.println("EXIST EXCEPTION");
                // TODO: localize
            }
        }
    }
    
    public void confirmChanges (ActionEvent event)
    {
        for(int i=0; i<getAccountList().size(); i++)
        {
            System.out.println(getAccountList().get(i).getUsername());
            staffAccountMgr.updateStatus(getAccountList().get(i));
        }
    }
}
